#include "ems.h"
#include "math.h"
#include "work.h"
#include "peripheral.h"

extern uint8_t Work_Mode,Work_Strength;
uint8_t Ems_Boost_Duty,phase_cnt=0,period_cnt1;
uint16_t EMS_VCCMAX_Target=30;
uint16_t EMS_VCC,EMS_VCC_Target=30;
float angle=0;
uint8_t angle_f=0,Ems_Wave,once_f=0;
uint16_t load_cnt1,load_cnt,sub_period=8*25,period_cnt;
boolean_t ems_f=0;
boolean_t EMS_Load=0;
float value; 
extern uint8_t off_cnt1,Beep_Time,Error_State;
extern boolean_t life_mode,age_mode,Touch_OK,HF_Load;
uint8_t unit1_time,unit2_time;
uint16_t sub1_period,sub2_period,unit1_period,unit2_period;
uint16_t unit1_duty,unit2_duty;

void EMS_Handler()	//程序卡死点，因为变量未初始化?(更换MCU正常)
{
		EMS_VCC = ADC_Convert(EMS_VCC_ADC_Channel);
		EMS_VCC = EMS_VCC *300.0f/4096;
		if(EMS_VCC_Target>EMS_VCCMAX_Target)
		EMS_VCC_Target=EMS_VCCMAX_Target;
		if(EMS_VCC>(EMS_VCC_Target+5))
		{
				if(Ems_Boost_Duty<100)
				Ems_Boost_Duty++;
		}
		else if(EMS_VCC<(EMS_VCC_Target-5))
		{
				if(Ems_Boost_Duty>0)
				Ems_Boost_Duty--;
//				else
//				Error_State|=EMS_ERROR;			//EMS��������쳣
		}
		if(Work_Mode<3)
		{
				// if(EMS_Load==1)
				if(Touch_OK==1)
				{
						if(Work_Mode==1)
						Ems_Wave=1;
						else if(Work_Mode==2)
						Ems_Wave=2;
						off_cnt1=0;
				}
				else
				{
						Ems_Wave=0;
				}
				EMS_Driver();
		}
		else
		{
				ems_f=0;
				Ems_Wave=0;
		}
}

void EMS_Detect()
{
		if(EMS_ISEN==0)
		{
				load_cnt1=0;
				if(load_cnt<1)
				load_cnt++;
				else
				{
						EMS_Load=1;
				}
		}
		else
		{
				load_cnt=0;
				if(load_cnt1<10)//200
				load_cnt1++;
				else
				{
						EMS_Load=0;
				}
		}
		if(life_mode||age_mode)
		EMS_Load=1;
}
//闪蝶第二个模式波形，单个脉冲高50us死区50us低50us，双波形，从低到高压500ms
//波形一持续3s，8ms一次脉冲，循环两次，间隔500ms，带载500R：23V，28V，32V，35V
//波形二持续3s，80ms两次脉冲，带载500R：28V，32V，35V，38V
void EMS_Driver()
{
		uint16_t unit_period,unit_duty;
		uint8_t unit_time,phase_time;
		switch(Ems_Wave)
		{
			case 0:
						phase_time=0;

						sub1_period=8*20;
						unit1_period=300;
						unit1_duty=300;
						unit1_time=1;
			
						EMS_VCCMAX_Target=35;
						break;
			case 1:
						phase_time=1;				//2种波形
				
						sub1_period=8*20;		//最小周期8ms
						unit1_period=350;		//波形周期3500ms
						unit1_duty=300;			//有效周期3000ms
						unit1_time=1;			//循环次数2次
						
						sub2_period=80*20;			
						unit2_period=350;
						unit2_duty=300;
						unit2_time=0;
						switch(Work_Strength)
						{
							case 0:if(phase_cnt==0)EMS_VCCMAX_Target=170;else EMS_VCCMAX_Target=170;break;	//110
							case 1:if(phase_cnt==0)EMS_VCCMAX_Target=190;else EMS_VCCMAX_Target=200;break;	//130
							case 2:if(phase_cnt==0)EMS_VCCMAX_Target=210;else EMS_VCCMAX_Target=230;break;	//150
							default:break;
						}
						break;
			case 2:
						phase_time=0;
			
						sub1_period=100*20;
						unit1_period=350;
						unit1_duty=300;
						unit1_time=0;
						switch(Work_Strength)
						{
							case 0:EMS_VCCMAX_Target=180;break;	//110
							case 1:EMS_VCCMAX_Target=210;break;	//130
							case 2:EMS_VCCMAX_Target=230;break;	//150
							default:break;
						}
						break;
			default:Ems_Wave=0;break;
		}
		switch(phase_cnt)	// 如果有两种波形，则两种波形的切换
		{
			case 0:
						sub_period=sub1_period;
						unit_period=unit1_period;
						unit_duty=unit1_duty;
						unit_time=unit1_time;
						break;
			case 1:
						sub_period=sub2_period;
						unit_period=unit2_period;
						unit_duty=unit2_duty;
						unit_time=unit2_time;
						break;
			default:break;
		}
	  period_cnt++;
		if(period_cnt>unit_period)	//波形持续周期
		{
				period_cnt=0;
				period_cnt1++;
				if(period_cnt1>unit_time)		//波形循环次数
				{
						period_cnt1=0;
						phase_cnt++;
						if(phase_cnt>phase_time)	//波形序号
						phase_cnt=0;
				}
		}
		else if(period_cnt>unit_duty)		//波形有效周期
		{
				ems_f=0;
				if(EMS_VCCMAX_Target>100)		//切换波形，电压从较低开始升压
				EMS_VCC_Target=EMS_VCCMAX_Target-50;
		}
		else
		{
				ems_f=1;			//波形有效输出
				if(EMS_VCC_Target<EMS_VCCMAX_Target)
				EMS_VCC_Target++;
		}
}
